package org.dyk.seamanshousefrontdesk.service.user;

import org.dyk.seamanshousefrontdesk.model.User;
import org.dyk.seamanshousefrontdesk.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;

@Service
public class UserServiceImpl implements UserService{
    @Autowired
    private UserRepository userRepository;

    /**
     * 登录验证
     * @param loginName
     * @param userPassword
     * @return
     */
    @Override
    public User login(String loginName, String userPassword) {
        User user = null;
        try {
            user = userRepository.findByUserLoginName(loginName);
        }catch (Exception e){
            e.printStackTrace();
        }
        if (null != user && userPassword.equals(user.getUserPassword())){
            return user;
        }
        return null;
    }

    /**
     * 注册验证
     * @param loginName
     * @param userPassword
     * @param userName
     * @return
     */
    @Override
    public boolean register(String loginName, String userPassword, String userName) {
        //验证是否有重复登录名，没有重复登录名便添加用户，否则返回提示
        User byUserLoginName = userRepository.findByUserLoginName(loginName);
        if (null == byUserLoginName){
            //验证通过
            User user = new User();
            user.setUserLoginName(loginName);
            user.setUserPassword(userPassword);
            user.setUserName(userName);
            userRepository.save(user);
            return true;
        }else {
            //验证未通过
            return false;
        }
    }

    /**
     * 修改验证
     * @param userLoginName
     * @param userPassword
     * @param userName
     * @return
     */
    @Override
    public User updataUser(int uid, String userLoginName, String userPassword, String userName) {
        try {
            //如果没有重复用户名
            User byUid = userRepository.findByUid(uid);
            byUid.setUserLoginName(userLoginName);
            byUid.setUserPassword(userPassword);
            byUid.setUserName(userName);
            userRepository.save(byUid);
            return byUid;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 用户头像修改验证
     * @param userImg
     * @return
     */
    @Override
    public User updataHead(int uid, MultipartFile userImg) {
        //获取图片名称并存入本地
        String originalFilename = userImg.getOriginalFilename();
        try {
            userImg.transferTo(new File("F:\\学习文件\\毕业设计\\mall-management-system\\src\\main\\resources\\static\\img\\user\\" + originalFilename));
            //拼接图片路径，并存入数据库
            String userHead = "/admin/img/user/" + originalFilename;
            //将头像存入登录用户
            User byUid = userRepository.findByUid(uid);
            byUid.setUserImg(userHead);
            userRepository.save(byUid);
            return byUid;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
